/**
 * larryCMS 自定义公共基础信息提示模块
 * Autor: Larry
 * Date: 17-04-25
 */
layui.define(['layer'],function(exports){
    "use strict";
	var $ = layui.jquery,
	    layer = layui.layer,
	    //修改后台服务器部署地址
        baseUrl = "";
	var CmsCommon = {
        /**
         * 抛出异常错误信息
         * @param {String} msg
         */
        cmsError: function(msg,title){
			parent.layer.alert(msg, {
				title: title,
				icon: 2,
				time: 0,
				resize: false,
				zIndex: layer.zIndex
			});
			return;
        },
        cmsInfo: function(msg,title){
            parent.layer.alert(msg, {
                title: title,
                icon: 6,
                time: 0,
                resize: false,
                zIndex: layer.zIndex
            });
            return;
        },
        cmsConfirm: function(){

        },
        //退出系统
        logOut: function (title, text, url,type, dataType, data, callback) {
            parent.layer.confirm(text, {
                title: title,
                resize: false,
                btnAlign: 'c'

            }, function () {

			location.href = url;
            });
        },
        cmsAjax: function(option){
        	  var defaults = {
        			  url : undefined,
        			  data : undefined,
        			  contentType : "application/json",// 发送信息至服务器时内容编码类型"application/x-www-form-urlencoded",
        			  dataType : "json",// 服务器返回json格式数据
        			  type : "POST",
        			  async : true,
        			  timeout : 60000,
        			  successFunc : undefined,
        			  errorFunc : undefined,
        			  error : undefined
        	  };
        	  var setting = $.extend({},defaults,option);
        	  var load_layer = null;
        	  if(!setting.url){
        		  setting.errorFunc && setting.errorFunc();
        		  return;
        	  }
			  $.ajax({
					url : baseUrl+setting.url,
					data : setting.data,
					contentType : setting.contentType,
					dataType : setting.dataType,
					type : setting.type,
					async : setting.async,
					timeout : setting.timeout,
                   processData : setting.processData,
					beforeSend : function() {
						load_layer = layer.msg("操作中...", {
							time : 0,
							shade : [ 0.3, '#393D49' ],
							icon : 4
						});
					},
					success : function(response) {
						if(!response||response.code!=='success'){
							if(setting.errorFunc!==undefined){
								setting.errorFunc(response);
								return;
							}
							layer.msg(response.msg?response.msg:"操作失败", {
								icon : 2
							});
							return;
						}
						setting.successFunc && setting.successFunc(response);
                        if(setting.url != '/login' && !sessionStorage.getItem('baffle-sys-user')){
                            layer.msg("权限已失效,将跳转至登陆页面", {
                                icon : 2
                            });
                            setTimeout(function(){
                                top.location.href="/logout";
                            },2000);
                        }
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						if(setting.error!==undefined){
							setting.error(XMLHttpRequest, textStatus, errorThrown);
							return;
						}
						layer.msg("系统错误", {
							icon : 2
						});
					},
					complete : function(XMLHttpRequest, textStatus) {
						load_layer && layer.close(load_layer);
					}
				});
        },
        cmsGetButtons : function(buttons){
        	if(buttons===undefined||buttons.length<=0){
        		return undefined;
        	}
        	var result = [];
        	for(var i in buttons){
    			for(var j in buttons[i]){
    				result.push(buttons[i][j].code);
    			}
    		}
        	return result;
        },
        cmsInArry : function(element,array){
        	for (var i = 0; i < array.length; i++) {
			    if (array[i] == element) {
			      return true;
			    }
			}
        	return false;
        },
        dateFormat : function(date, format) {
        	format = typeof (format) == "string" ? format : "yyyy-MM-dd";
    		if (date instanceof Date) {
    			// do nothing
    		} else if (!isNaN(date) && date != "") {
    			date = new Date(date);
    		} else {
    			return date;
    		}
    		var o = {
    			"M+" : date.getMonth() + 1, // month
    			"d+" : date.getDate(), // day
    			"h+" : date.getHours(), // hour
    			"m+" : date.getMinutes(), // minute
    			"s+" : date.getSeconds(), // second
    			"q+" : Math.floor((date.getMonth() + 3) / 3), // quarter
    			"S" : date.getMilliseconds()
    		};
    		if (/(y+)/.test(format))
    			format = format.replace(RegExp.$1, (date.getFullYear() + "")
    					.substr(4 - RegExp.$1.length));
    		for ( var k in o)
    			if (new RegExp("(" + k + ")").test(format))
    				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
    						: ("00" + o[k]).substr(("" + o[k]).length));
    		return format;
    	},
    	hoverTip : function(option){
    		/*使用方式
    		common.hoverTip({
				element : ".tipClassSoftware"
		    });
    		common.hoverTip();*/
    		var defaults = {
    			element:".tipClassSoftware",//默认class名称
    			time:750
      	    };
      	    var setting = $.extend({},defaults,option);
      	    if(!setting.element){
      	    	return;
      	    }
      	    var $this=$(setting.element);
      	    /*$(setting.element).on({
      	    	"mouseenter":function(){
      	    	    clearTimeout(timer);
      	    	    timer=setTimeout(function(){
      	    	        //这里触发hover事件
      	    	    },setting.time);
      	    	},
      	    	"mouseleave":function(){
      	    	    clearTimeout(timer);
      	    	}
      	    });*/
      	  $this.each(function(){
	      	   var $inThis = $(this),timer;
	      	   $inThis.unbind("mouseenter mouseleave");
	      	   $inThis.hover(function(){
	      		   var inThis=this;
	      		   timer =setTimeout(function(){
	    	    		if(inThis.innerText || inThis.innerHTML || inThis.value){
	    					layer.tips(inThis.innerText || inThis.innerHTML || inThis.value, inThis,{tips:1,time:0});
	    				}
	    			},setting.time);
	    	    },function(){
	    	    	clearTimeout(timer);
	    	    	layer.closeAll('tips');
	    	    });
      	  });
      	  
    	}
        
        
        
        
        
        
        
        

	};

	exports('common',CmsCommon);
})